package com.webratio.units.custom.googletranslateunit;
import java.util.Collections;
import java.util.Map;
import org.dom4j.Element;
import com.webratio.rtx.RTXContentUnitService;
import com.webratio.rtx.RTXException;
import com.webratio.rtx.RTXManager;
import com.webratio.rtx.core.AbstractService;
import com.webratio.rtx.core.BeanHelper;
import com.webratio.rtx.core.DescriptorHelper;
import com.webratio.units.custom.googlemapunit.GoogleMapMarker;
import com.webratio.units.custom.googlemapunit.GoogleMapUnitBean;


public class GoogleTranslateUnitService extends AbstractService implements RTXContentUnitService{

	private int width;
	private int height;
	private int mashUpCode;

	public GoogleTranslateUnitService(String id, RTXManager mgr, Element descr) throws RTXException {
		super(id, mgr, descr);

		Element currentUnit = descr.element("GoogleTranslateUnit");

		// valores predefinidos
		width = 160;
		height = 60;
		mashUpCode=0;
		

	}

	public Object computeParameterValue(String outputParamName, Map pageContext, Map sessionContext) throws RTXException {
		// TODO Auto-generated method stub
		return null;
	}

	public Object execute(Map pageContext, Map sessionContext) throws RTXException {

		// bean inizialization
		GoogleTranslateUnitBean unitBean = getUnitBean(pageContext, sessionContext);
		if (unitBean == null) {
			return null;
		}

		return unitBean;
	}


	private GoogleTranslateUnitBean getUnitBean(Map pageContext, Map sessionContext)
	throws RTXException {

		GoogleTranslateUnitBean unitBean = (GoogleTranslateUnitBean) pageContext
		.get('_' + getId());
		if (unitBean == null || unitBean.getDataSize() == 0) {
			unitBean = createUnitBean(pageContext, sessionContext);
		}

		/*Estas sgtes lineas son las encargadas de sacar los valores de la URL :P*/
		int widthTmp = Integer.parseInt(BeanHelper.asString(pageContext
				.get(getId() + ".width")));
		int heightTmp = Integer.parseInt(BeanHelper.asString(pageContext
				.get(getId() + ".height")));
		int mashUpCodeTmp = Integer.parseInt(BeanHelper.asString(pageContext
				.get(getId() + ".mashUpCode")));
		
		
		// Insert of the values
		unitBean.setWidth(widthTmp);
		unitBean.setHeight(heightTmp);
		unitBean.setMashUpCode(mashUpCodeTmp);


		pageContext.put('_' + getId(), unitBean);

		return unitBean;
	}
	private GoogleTranslateUnitBean createUnitBean(Map pageContext,
			Map sessionContext) throws RTXException {
		GoogleTranslateUnitBean bean = new GoogleTranslateUnitBean();

		bean.setDataSize(0);
		bean.setData(Collections.EMPTY_LIST);

		return bean;
	}

	public void dispose() {
		// TODO Auto-generated method stub
	}

}